//
//  LGConsultTipView.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/5/28.
//

#import "LGConsultTipView.h"
#import "TIMDefine.h"
@interface LGConsultTipView()
/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGConsultTipView

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

/*
 TIMCustom_02   咨询已开始，可持续时长24H

 TIMCustom_03  本次咨询已结束
 */

-(void)fillWithData:(LGConsultTipData *)data{
    data.showName = NO;
    data.showAvatar = NO;
    [super fillWithData:data];
    NSString *businessID = LGNSString(data.customDic[@"businessID"]);
    NSString *content = LGNSString(data.customDic[@"desc"][@"content"]);
    self.contentLabel.text = content;
    CGFloat contentH = [content textHeightWithFont:LGFont(16) maxWidth:viewPix(200)];
    contentH = (contentH<viewPix(24))?viewPix(24):contentH;
    CGFloat contentW = [content textWidthWithFont:LGFont(16) maxHeight:MAXFLOAT];
    contentW = (contentW>viewPix(160))?viewPix(160):contentW;
    [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(contentW+viewPix(40)));
        make.height.equalTo(@(contentH));
    }];
}

+(CGFloat)getEstimatedHeight:(LGConsultTipData *)data{
    NSString *businessID = LGNSString(data.customDic[@"businessID"]);
    NSString *content = @"";
    if ([businessID isEqualToString:@"TIMCustom_02"]) {
        content = @"咨询已开始，可持续时长24H";
    }else if ([businessID isEqualToString:@"TIMCustom_03"]){
        content = @"本次咨询已结束";
    }
    CGFloat contentH = [content textHeightWithFont:LGFont(16) maxWidth:viewPix(200)];
    contentH = (contentH<viewPix(24))?viewPix(24):contentH;
    return contentH;
}

+(CGSize)getContentSize:(LGConsultTipData *)data{
    CGFloat height = [LGConsultTipView getEstimatedHeight:data];
    return CGSizeMake(Screen_W-112, height);
}

+(CGFloat)getHeight:(LGConsultTipData *)data withWidth:(CGFloat)width{
    CGFloat height = [LGConsultTipView getEstimatedHeight:data];
    return height+35;
}

-(void)createSubView{
    [self.container addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.container);
        make.width.equalTo(@(0));
        make.height.equalTo(@(0));
    }];
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#968F8F" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:0];
        _contentLabel.backgroundColor = [UIColor colorWithString:@"#EBEBEB"];
        _contentLabel.cornerRidus = viewPix(12);
    }
    return _contentLabel;
}





@end


#pragma mark
#pragma mark ====> LGConsultTipData

@implementation LGConsultTipData
+ (LGConsultTipData *)getCellData:(V2TIMMessage *)message{
    LGConsultTipData *cellData = [[LGConsultTipData alloc] initWithDirection:message.isSelf ? MsgDirectionOutgoing : MsgDirectionIncoming];
    cellData.showName = NO;
    cellData.showAvatar = YES;
    cellData.innerMessage = message;
    cellData.msgID = message.msgID;
    cellData.avatarUrl = [NSURL URLWithString:message.faceURL];
    cellData.customDic = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    return cellData;
}

/**
 * Getting the display string according to the message
 */
+(NSString *)getDisplayString:(V2TIMMessage *)message{
    NSDictionary *param = [NSJSONSerialization JSONObjectWithData:message.customElem.data options:NSJSONReadingAllowFragments error:nil];
    if (!param || ![param isKindOfClass:[NSDictionary class]]) {
        return @"[自定义消息]";
    }
    NSString *businessID = param[BussinessID];
    if([businessID isEqualToString:@"TIMCustom_02"]){
        return @"[咨询已开始]";
    }else if ([businessID isEqualToString:@"TIMCustom_03"]){
        return @"[咨询已结束]";
    }
    
    return @"[自定义消息]";
}

@end
